﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RayDen.Library.Components.Color.Types;
using RayDen.Library.Core.Components;
using RayDen.Library.Core.Primitives;
using RayDen.Library.Data;
using RayDen.RayEngine;
using RayDen.RayEngine.Corrector.Managers;

namespace RayEngine.Spectral.Entities.Textures
{
    public class TextureFactory : Singleton<TextureFactory>
    {
        private int textureId = 0;
        protected IValueTexture[] Textures;
        protected Dictionary<string, int> texMap;

        protected int NextId
        {
            get { return textureId++; }
        }


        public TextureFactory()
        {
            Textures = new IValueTexture[2000];
            texMap = new Dictionary<string, int>(2000);
        }

        public IValueTexture GetTexture(int id)
        {
            return Textures[id];
        }

        public IValueTexture CreateConstTexture(ref RgbSpectrum texVal)
        {
            var texName = texVal.ToString();
            if (texMap.ContainsKey(texName))
                return Textures[texMap[texName]];
            var tex = new ConstantTexture(NextId, ColorManager.Instance.Convert(ref texVal));
            Textures[textureId - 1] = tex;
            texMap.Add(texName, textureId - 1);
            return tex;
        }

        public IValueTexture CreateConstTexture(IColorType texVal)
        {
            var tex = new ConstantTexture(NextId, texVal);
            Textures[textureId - 1] = tex;
            return tex;
        }

        public IValueTexture CreateConstTexture(ref SampledSpectrum texVal)
        {
            var texName = texVal.ToString();
            if (texMap.ContainsKey(texName))
                return Textures[texMap[texName]];
            var tex = new ConstantTexture(NextId, ColorManager.Instance.Convert(texVal));
            Textures[textureId - 1] = tex;
            texMap.Add(texName, textureId - 1);
            return tex;
        }

        public IValueTexture CreateImageTexture(RgbSpectrumTexture texVal)
        {
            var texName = texVal.ToString();
            if (texMap.ContainsKey(texName))
                return Textures[texMap[texName]];
            var tex = new ImageTexture(NextId, texVal);
            Textures[textureId - 1] = tex;
            texMap.Add(texName, textureId - 1);

            return tex;
        }
    }
}
